package org.jhulk.states;

import org.jhulk.input.KeyboardInputPlayControl;
import org.jhulk.sprites.Marine;
import org.jhulk.sprites.Squad;
import org.jhulk.utils.GameUtils;
import org.jhulk.utils.TileMapUtils;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class PlayState extends BasicGameState {

	/** The ID given to this state */
	public static final int ID = 2;
	
	private GameCache cache;
	
    /** The game holding this state */ 
    private StateBasedGame game; 
    
    private KeyboardInputPlayControl playControl = new KeyboardInputPlayControl();
    
    public PlayState(GameCache cache)
    {
    	this.cache = cache;
    }
    
	@Override
	public int getID() {
		return ID;
	}
	
	public void init(GameContainer arg0, StateBasedGame arg1)
			throws SlickException {}

	public void render(GameContainer c, StateBasedGame sbg, Graphics g) 
		throws SlickException {
		
		cache.getMap().render(0, 0);

		//If the cursor is over a valid tile, highlight the tile
		if(cache.getHovTileId() != 0)
			g.drawRect(
					cache.getMapXForTile().floatValue(), 
					cache.getMapYForTile().floatValue(), 
					cache.getMap().getTileWidth(), 
					cache.getMap().getTileHeight());
		
		//Draw a rectangle around the selected character
		if(cache.getSelectedChar() != null)
		{
			g.setColor(Color.cyan);
			Marine selChar = cache.getSelectedChar();
			g.drawRect(
					selChar.getX(), 
					selChar.getY(), 
					cache.getMap().getTileWidth(), 
					cache.getMap().getTileHeight());
		}
		
		//Loop through all characters
		
		////Loop through all squads
		for(Squad s : cache.getSquads()) 
		{
			s.renderSquad();
		}

	}

	public void update(GameContainer arg0, StateBasedGame arg1, int arg2)
			throws SlickException {
		// TODO Auto-generated method stub

	}

	 public void mouseReleased(int button, int x, int y)
    {
    	cache.setCurrTile( TileMapUtils.getMapTileFromClick(x, y, cache.getMap()) );

    	determineCharAtClick();
        
    }
	 
    private void determineCharAtClick()
    {
    	//loop through the squads for any character that has those coords
    	//TODO: Need to change this to look in ALL characters, not just squads
    	for(Squad s : cache.getSquads())
    	{
    		for(Marine m : s.getAllUnits())
    		{
    			if(m.getX() == cache.getCurrTile().getMapX() && m.getY() == cache.getCurrTile().getMapY())
    			{
    				cache.setSelectedChar( m );
    				System.out.println("Selected character is: " + m.getName());
    			}
    		}
    	}
    }
    
    public void keyPressed(int key, char c)
    {
    	playControl.keyPressed(key, c, cache);
    }
    
    public void keyReleased(int key, char c)
    {
        if(key == Input.KEY_LCONTROL || key == Input.KEY_RCONTROL)
            cache.setCtrlPressed( false );
    }
    
    
    public void mouseMoved(int oldx, int oldy, int newx, int newy)
    {
    	 cache.setHovTileX( TileMapUtils.getTileXfromMapX(newx, cache.getMap()) );
	     cache.setHovTileY( TileMapUtils.getTileYfromMapY(newy, cache.getMap()) );
	     
	     cache.setHovTileId( cache.getMap().getTileId(cache.getHovTileX(), cache.getHovTileY(), 0) );
	     
	     cache.setMapXForTile( new Double(TileMapUtils.getTileXBorderFromMapX(newx, cache.getMap())) );
	     cache.setMapYForTile( new Double(TileMapUtils.getTileYBorderFromMapY(newy, cache.getMap())) );
    }
	
}
